
cd " "	

use census_pool, clear
keep if age>=25 & age<=60
keep if ybirth>=1940 & ybirth<=1990
gen female = 1-male
egen prov_year_birth = group(provbirth ybirth)
egen prov_birth_female = group(provbirth male)
egen ybirth_female = group(ybirth female)
egen prov_qbirth = group(provbirth qbirth)
egen sample_female = group(sampleyear female)
gen han_female = han*female
gen Q1_female = Q1*(1-male)
gen Q2_female = Q2*(1-male)
gen Q3_female = Q3*(1-male)
gen Q4_female = Q4*(1-male)
bys provbirth ybirth: egen primary_mean = mean(primary)

// Table 1
summ yedu junior high if male==0
summ yedu junior high if male==1

summ mincome admin_soe insur_u  if male==0
summ mincome admin_soe insur_u if male==1


// Table 4
foreach y of varlist yedu junior high {
						
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
		
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_f
			
		    suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
}

// Table D2
gen famine = (ybirth>=1959 & ybirth<=1962)
gen Q2_famine = Q2*famine
gen Q3_famine = Q3*famine
gen Q4_famine = Q4*famine
	
	reghdfe yedu Q2 Q3 Q4 Q2_famine Q3_famine Q4_famine male han primary_mean  if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	reghdfe yedu Q2 Q3 Q4 Q2_famine Q3_famine Q4_famine male han primary_mean  if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	
	test Q2_famine Q3_famine Q4_famine

// Table D3 
sum yedu if male==0 & ybirth<=1978
sum yedu if male==1 & ybirth<=1978
sum yedu if male==0 & ybirth<=1970
sum yedu if male==1 & ybirth<=1970
sum yedu if male==0 & (ybirth<=1958 | ybirth>=1962)
sum yedu if male==1 & (ybirth<=1958 | ybirth>=1962)

	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==0 & ybirth<=1978, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==1 & ybirth<=1978, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	
	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==0 & ybirth<=1970, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==1 & ybirth<=1970, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	
	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==0 & (ybirth<=1958 | ybirth>=1962), absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	reghdfe yedu Q2 Q3 Q4 male han primary_mean if male==1 & (ybirth<=1958 | ybirth>=1962), absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

// Table D4

merge m:1 mbirth using maternal_month
drop if _merge==2
drop _merge
	
global ma_char ma_yedu ma_age ma_migrant_hukou ma_urban ma_numbir ma_brim_yes
	
foreach y of varlist yedu junior high {
						
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean $ma_char if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean $ma_char if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
		
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean $ma_char i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean $ma_char i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
}

	
// Table A4
		
sum yedu junior high lmincome admin_soe insur_u	
reghdfe  yedu c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe junior c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)	
reghdfe  high c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe  lmincome c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe  admin_soe c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe  insur_u c.Q2##c.female c.Q3##c.female c.Q4##c.female   han primary_mean , absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)	
	
	
// Table 5

	foreach y of varlist lmincome admin_soe insur_u  {
			
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			
			quietly reg  `y' Q2 Q3 Q4 male han  primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han  primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]Q4 = [`y'_f_mean]Q4)  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3)
				
	}

// Table A7	
	foreach y of varlist lmincome admin_soe insur_u  {
			
			summ `y'
			summ `y' if male==1
			summ `y' if male==0
			
			reghdfe  `y' Q2 Q3 Q4 male han yedu primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' Q2 Q3 Q4 male han yedu primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' Q2 Q3 Q4 male han yedu primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han yedu primary_mean  i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]Q4 = [`y'_f_mean]Q4)  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3)				
	}
	

	
// Table E1	
	
	foreach x of numlist 11 12 13 14 15 21 22 23 31 32 33 34 35 36 37 41 42 43 44 45 46 50 51 52 53 54 61 62 63 64 65 {				
		
		reghdfe yedu Q1 Q3 Q4  han male primary_mean if provbirth==`x', absorb( ybirth sampleyear) cluster(prov_year_birth)
		reghdfe yedu Q1 Q3 Q4  han primary_mean if male==1 & provbirth==`x', absorb( ybirth sampleyear) cluster(prov_year_birth)
		reghdfe yedu Q1 Q3 Q4  han primary_mean if male==0 & provbirth==`x', absorb( ybirth sampleyear) cluster(prov_year_birth)
				
		quietly reg  yedu Q1 Q3 Q4  han primary_mean i.ybirth i.sampleyear if male==1 & provbirth==`x'
		est store yedu_m_`x'
		
		quietly reg  yedu Q1 Q3 Q4  han primary_mean i.ybirth i.sampleyear if male==0 & provbirth==`x'
		est store yedu_f_`x'
		
		quietly suest yedu_m_`x' yedu_f_`x',  vce(cluster ybirth) 
		test [yedu_m_`x'_mean]Q4 = [yedu_f_`x'_mean]Q4		
	} 	



	
merge m:1 provbirth qbirth using prov_pq_m9
drop if _merge==2
drop _merge


merge m:1 mbirth using maternal_month
drop if _merge==2
drop _merge

merge m:1 prov qbirth using prov_qob_maternal
drop if _merge==2
drop _merge



// Table A10
gen Q4_prov = 0

replace Q4_prov = 1 if (mbirth>=8 & mbirth<=10) & (provbirth<=15 | provbirth==36 | provbirth==46 | provbirth==43 | provbirth==44 | provbirth==62 | provbirth==63 | provbirth==64)
replace Q4_prov = 1 if (mbirth>=10 & mbirth<=12) & (provbirth==21 | provbirth==22 | provbirth==23 | provbirth==31 | provbirth==32 | provbirth==33 | provbirth==34 | provbirth==35 | provbirth==50 | provbirth==51 | provbirth==52 | provbirth==53 | provbirth==42 | provbirth==45)
replace Q4_prov = 1 if (mbirth>=6 & mbirth<=8) & (provbirth==37 | provbirth==41 | provbirth==54 | provbirth==61 | provbirth==65)

bys provbirth: sum Q4_prov

reghdfe  yedu Q4_prov male han primary_mean if male==0, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, replace bdec(3) sdec(3) 
reghdfe  yedu Q4_prov male han primary_mean if male==1, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, append  bdec(3) sdec(3) 
reghdfe  junior Q4_prov male han primary_mean if male==0, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, append  bdec(3) sdec(3) 
reghdfe  junior Q4_prov male han primary_mean if male==1, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, append  bdec(3) sdec(3) 
reghdfe  high Q4_prov male han primary_mean if male==0, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, append  bdec(3) sdec(3) 
reghdfe  high Q4_prov male han primary_mean if male==1, absorb(provbirth ybirth sampleyear ) cluster(prov_year_birth)
	outreg2 using tables/q4prov.rtf, append  bdec(3) sdec(3) 

	foreach y of varlist yedu junior high {			

			qui reg `y'  Q4_prov male han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
				est store `y'_m
			qui reg `y'  Q4_prov male han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
				est store `y'_f
	
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]Q4_prov = [`y'_f_mean]Q4_prov)   				
	}	


// Table A11

recode prov (15 64 63 23 22 21 54 62 65 14 61 11 52 12 13 = 1), gen(mci)
replace mci = 2 if mci!=1

summ yedu if mci==1 & male==0
summ yedu if mci==1 & male==1
summ yedu if mci==2 & male==0
summ yedu if mci==2 & male==1

reghdfe  yedu  Q2 Q3 Q4 han primary_mean if mci==1 & male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe  yedu  Q2 Q3 Q4 han primary_mean if mci==1 & male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

reghdfe  yedu  Q2 Q3 Q4 han primary_mean if mci==2 & male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
reghdfe  yedu  Q2 Q3 Q4 han primary_mean if mci==2 & male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

foreach y of varlist yedu  {
								
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if mci==1 & male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if mci==1 &  male==0
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
}

foreach y of varlist yedu  {
								
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if mci==2 & male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if mci==2 &  male==0
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
}	

merge m:1 qbirth using qbirth_var
drop if _merge==2
drop _merge

merge m:1 provbirth qbirth using prov_pq_m9
drop if _merge==2
drop _merge

merge m:1 provbirth qbirth using prov_quarter_a
drop if _merge==2
drop _merge


// Table 7
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' resource_m4_aq    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_aq    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_aq = [`y'_f_mean]resource_m4_aq)   
				
	}
	
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' resource_m4_pq   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_pq   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_pq = [`y'_f_mean]resource_m4_pq)   
				
	}
	
// Table A13
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' m_resource_m4_m9_aq    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' m_resource_m4_m9_aq    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' m_resource_m4_m9_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' m_resource_m4_m9_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]m_resource_m4_m9_aq = [`y'_f_mean]m_resource_m4_m9_aq)   
				
	}

	foreach y of varlist yedu junior high {
			
			reghdfe  `y' m_resource_m4_m9_pq   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' m_resource_m4_m9_pq   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' m_resource_m4_m9_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' m_resource_m4_m9_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]m_resource_m4_m9_pq = [`y'_f_mean]m_resource_m4_m9_pq)   
				
	}

	
// Table A14
	foreach y of varlist lmincome admin_soe insur_u {
			
			reghdfe  `y' resource_m4_aq    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_aq    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_aq = [`y'_f_mean]resource_m4_aq)   
				
	}

	foreach y of varlist lmincome admin_soe insur_u {
			
			reghdfe  `y' resource_m4_pq   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_pq   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_pq   male han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_pq   male han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_pq = [`y'_f_mean]resource_m4_pq)   
				
	}

	
merge m:1 provbirth mbirth using prov_month_pm
drop _merge

rename mbirth month
merge m:1 month using month_var_pca
drop _merge
rename month mbirth 
	
// Table A15
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' resource_m4_am    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_am    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_am  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_am  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_am = [`y'_f_mean]resource_m4_am)   
				
	}

	foreach y of varlist yedu junior high {
			
			reghdfe  `y' resource_m4_pm   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_pm   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_pm   male han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_pm   male han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_pm = [`y'_f_mean]resource_m4_pm)   
				
	}
	
// Table A16	
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' food_m4_aq    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' food_m4_aq    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' food_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' food_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]food_m4_aq = [`y'_f_mean]food_m4_aq)   
				
	}

	foreach y of varlist yedu junior high {
			
			reghdfe  `y' food_m4_pq   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' food_m4_pq   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' food_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' food_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]food_m4_pq = [`y'_f_mean]food_m4_pq)   
				
	}
	
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' working_m4_aq    han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' working_m4_aq    han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' working_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' working_m4_aq  han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]working_m4_aq = [`y'_f_mean]working_m4_aq)   
				
	}

	foreach y of varlist yedu junior high {
			
			reghdfe  `y' working_m4_pq   han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' working_m4_pq   han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' working_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_m
			
			quietly reg  `y' working_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]working_m4_pq = [`y'_f_mean]working_m4_pq)   
				
	}
	

	
	merge m:1 mbirth using disease_month
	drop if _merge==2
	drop _merge
 	
	foreach var of varlist nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis {
		egen std_`var' = std(`var')
		replace `var'  = std_`var'
		drop std_`var'
	}
	
	merge m:1 provbirth mbirth using prov_month_infl
	drop if _merge==2
	drop _merge
	replace infl = 0 if infl==.
	replace std_infl = 0 if infl==.
		
	
	// Table G1
	foreach y of varlist yedu junior high {
							
				reghdfe  `y' Q2 Q3 Q4 male han primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
				reghdfe  `y' Q2 Q3 Q4 male han primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			
				quietly reg  `y' Q2 Q3 Q4 male han primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl i.provbirth i.ybirth i.sampleyear if male==1
				est store `y'_m
				
				quietly reg  `y' Q2 Q3 Q4 male han primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl i.provbirth i.ybirth i.sampleyear if male==0
				est store `y'_f
				
				suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
				test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
	}

	
	// Table G2
	foreach y of varlist yedu junior high {
			
			reghdfe  `y' resource_m4_pq   han primary_mean primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl  if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' resource_m4_pq   han primary_mean primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl  if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)

			quietly reg  `y' resource_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl if male==0
			est store `y'_m
			
			quietly reg  `y' resource_m4_pq   han primary_mean i.provbirth i.ybirth i.sampleyear primary_mean nd_measles nd_bad nd_malaria nd_dengue nd_brucellosis nd_tuberculosis std_infl if male==1
			est store `y'_f
			
			quietly suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test ([`y'_m_mean]resource_m4_pq = [`y'_f_mean]resource_m4_pq)   
				
	}

use census_2020, clear
keep if ybirth>=1991 & ybirth<=1995
gen female = 1-male
egen prov_year_birth = group(provbirth ybirth)
bys provbirth ybirth: egen primary_mean = mean(primary)

// Table A5

summ yedu junior high if male==0
summ yedu junior high if male==1

foreach y of varlist yedu junior high {
						
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==0, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
			reghdfe  `y' Q2 Q3 Q4 male han primary_mean if male==1, absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
		
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if male==1
			est store `y'_m
			
			quietly reg  `y' Q2 Q3 Q4 male han primary_mean i.provbirth i.ybirth i.sampleyear if male==0
			est store `y'_f
			
		    suest `y'_m `y'_f,  vce(cluster prov_year_birth) 
			test  ([`y'_m_mean]Q2 = [`y'_f_mean]Q2) ([`y'_m_mean]Q3 = [`y'_f_mean]Q3) ([`y'_m_mean]Q4 = [`y'_f_mean]Q4) 				
}
	

	
// Figure 1(a)

	use census_pool, clear
	keep if age>=25 & age<=60
	keep if ybirth>=1940 & ybirth<=1990
	bys ybirth: egen m_yedu = mean(yedu)
	collapse (mean) yedu m_yedu, by(ybirth qbirth)
	replace yedu = yedu / m_yedu
	twoway (connected yedu ybirth if qbirth==1, color(green) msymbol(triangle) msize(small) ) ///
		   (connected yedu ybirth if qbirth==2, color(orange) msymbol(X) msize(small) ) ///
		   (connected yedu ybirth if qbirth==3, color(maroon) msymbol(Oh) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4, color(blue) msymbol(Dh) msize(small) ) ///
		   ,legend(label(1 Q1) label(2 Q2) label(3 Q3) label(4 Q4) cols(4)) ///
		   xtitle("Year of birth (YOB)") ytitle("sch. years for the QOB / sch. years for the YOB")  ///
		   xline(1949 1959 1962  1978, lcolor(gs8) yline(1,lcolor(gs8)) ) 

// Figure 1(b)
	
	use census_pool, clear
	keep if age>=25 & age<=60
	keep if ybirth>=1940 & ybirth<=1990
	bys ybirth male: egen m_yedu = mean(yedu)
	collapse (mean) yedu m_yedu, by(ybirth qbirth male)
	keep if qbirth==2 | qbirth==4
	replace yedu = yedu / m_yedu
	keep if ybirth>=1940
	twoway (connected yedu ybirth if qbirth==2 & male==1, color(green) msymbol(triangle) msize(small) ) ///
		   (connected yedu ybirth if qbirth==2 & male==0, color(orange) msymbol(X) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4 & male==1, color(maroon) msymbol(Oh) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4 & male==0, color(blue) msymbol(Dh) msize(small) ) ///
		   ,legend(label(1 Q2_male) label(2 Q2_female) label(3 Q4_male) label(4 Q4_female) cols(4)) ///
		   xtitle("Year of birth") ytitle("sch. years for the QOB / sch. years for the YOB") 
 	
// Figure A1(a)

	use census_pool, clear
	keep if age>=25 & age<=60
	keep if ybirth>=1940 & ybirth<=1990
	bys ybirth: egen m_yedu = mean(yedu)
	collapse (mean) yedu m_yedu, by(ybirth qbirth)
	replace yedu = yedu - m_yedu
	twoway (connected yedu ybirth if qbirth==1, color(green) msymbol(triangle) msize(small) ) ///
		   (connected yedu ybirth if qbirth==2, color(orange) msymbol(X) msize(small) ) ///
		   (connected yedu ybirth if qbirth==3, color(maroon) msymbol(Oh) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4, color(blue) msymbol(Dh) msize(small) ) ///
		   ,legend(label(1 Q1) label(2 Q2) label(3 Q3) label(4 Q4) cols(4)) ///
		   xtitle("Year of birth (YOB)") ytitle("sch. years for the QOB - sch. years for the YOB")  ///
		   xline(1949 1959 1962  1978, lcolor(gs8) yline(1,lcolor(gs8)) ) 

// Figure A1(b)
	
	use census_pool, clear
	keep if age>=25 & age<=60
	keep if ybirth>=1940 & ybirth<=1990
	bys ybirth male: egen m_yedu = mean(yedu)
	collapse (mean) yedu m_yedu, by(ybirth qbirth male)
	keep if qbirth==2 | qbirth==4
	replace yedu = yedu - m_yedu
	twoway (connected yedu ybirth if qbirth==2 & male==1, color(green) msymbol(triangle) msize(small) ) ///
		   (connected yedu ybirth if qbirth==2 & male==0, color(orange) msymbol(X) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4 & male==1, color(maroon) msymbol(Oh) msize(small) ) ///
		   (connected yedu ybirth if qbirth==4 & male==0, color(blue) msymbol(Dh) msize(small) ) ///
		   ,legend(label(1 Q2_male) label(2 Q2_female) label(3 Q4_male) label(4 Q4_female) cols(4)) ///
		   xtitle("Year of birth") ytitle("sch. years for the QOB - sch. years for the YOB") 

// Figure A2

use census_pool, clear
keep if age>=25 & age<=60
keep if ybirth>=1940 & ybirth<=1990
foreach x of numlist 1940/1990 {
	reghdfe  yedu  Q1 Q3 Q4 han  if male==1 & ybirth==`x', absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	gen coef_m`x' = _b[Q4]
}
foreach x of numlist 1940/1990 {
	reghdfe  yedu  Q1 Q3 Q4 han  if male==0 & ybirth==`x', absorb(provbirth ybirth sampleyear) cluster(prov_year_birth)
	gen coef_f`x' = _b[Q4]
}
keep coef_*
duplicates drop
gen one=1
reshape long coef_m coef_f, i(one) j(year)
twoway connected coef_m coef_f year
	
	
// Figure 4	
use census_pool, clear
keep if age>=25 & age<=60
keep if ybirth>=1940 & ybirth<=1990
bys provbirth ybirth: gen num = _N
bys provbirth ybirth qbirth: egen yedu_q = mean(yedu)
bys provbirth ybirth qbirth: egen yedu_q_m = mean(yedu) if male==1
bys provbirth ybirth qbirth: egen yedu_q_f = mean(yedu) if male==0
bys provbirth ybirth qbirth: egen min_yedu_q_m = min(yedu_q_m)
bys provbirth ybirth qbirth: egen min_yedu_q_f = min(yedu_q_f)
replace yedu_q_m = min_yedu_q_m
replace yedu_q_f = min_yedu_q_f
keep provbirth ybirth qbirth yedu_q yedu_q_m yedu_q_f num
duplicates drop
reshape wide yedu_q*, i(ybirth provbirth) j(qbirth)
rename ybirth year
rename provbirth prov
replace year = year - 1
merge m:1 prov year using prov_crop
xtset prov year
gen lcrop = log(crop)
gen lcroppc = log(crop/pop)
gen yedu4_2_m = yedu_q_m4 - yedu_q_m2
gen yedu4_2_f = yedu_q_f4 - yedu_q_f2
drop if prov==11 | prov==12 | prov==31  
drop if prov==15 | prov==54 | prov==65 | prov==63
drop if year<1952
reghdfe yedu4_2_f lcrop [w=num], absorb(year prov ) vce(cluster prov)
reghdfe yedu4_2_m lcrop [w=num], absorb(year prov ) vce(cluster prov)
binscatter yedu4_2_f  yedu4_2_m lcrop [w=num], absorb(prov) control(i.year) nbins(19)

// Table D1
use census_pool, clear
keep if sampleyear == 2000
drop if ybirth<1940
drop if ybirth>1990
gen pop = 1
collapse (sum) pop, by(qbirth ybirth male prov)
save pop_temp, replace
keep prov
duplicates drop
expand 51
bys prov: gen ybirth = 1939 + _n
expand 4
bys prov ybirth: gen qbirth = _n
expand 2 
bys prov ybirth qbirth: gen male = _n -1 
merge 1:1 prov ybirth qbirth male using pop_temp
replace pop = 0 if _merge==1
drop _merge
egen prov_ybirth = group(prov ybirth)
egen male_prov   = group(male prov)
gen Q1 = (qbirth==1)
gen Q2 = (qbirth==2)
gen Q3 = (qbirth==3)
gen Q4 = (qbirth==4)
gen Q2_male = Q2*male
gen Q3_male = Q3*male
gen Q4_male = Q4*male
gen Q2_ybirth = Q2*ybirth
gen Q3_ybirth = Q3*ybirth
gen Q4_ybirth = Q4*ybirth
gen Q2_male_ybirth = Q2*male*ybirth
gen Q3_male_ybirth = Q3*male*ybirth
gen Q4_male_ybirth = Q4*male*ybirth
gen male_ybirth = male*ybirth
bys prov: egen pop_prov = total(pop)
bys prov ybirth: egen pop_prov_ybirth = total(pop)
replace ybirth = 2000 - ybirth
reg   pop Q2_male_ybirth Q3_male_ybirth Q4_male_ybirth   ///
			Q2_ybirth Q3_ybirth Q4_ybirth male_ybirth Q2_male Q3_male Q4_male ///
			Q2 Q3 Q4 male i.prov i.ybirth ,   cluster(prov)
test Q2_male_ybirth Q3_male_ybirth Q4_male_ybirth  

reghdfe   pop Q2_male_ybirth Q3_male_ybirth Q4_male_ybirth   ///
			Q2_ybirth Q3_ybirth Q4_ybirth male_ybirth Q2_male Q3_male Q4_male ///
			Q2 Q3 Q4 male, absorb(prov#qbirth ybirth#qbirth  )  cluster(prov)
test Q2_male_ybirth Q3_male_ybirth Q4_male_ybirth  
